package com.itheima.test6;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.UUID;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ServiceDemo {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(10000);
        ExecutorService service = Executors.newCachedThreadPool();
        while (true) {
            Socket accept = serverSocket.accept();
            service.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        //网络读
                        BufferedReader br = new BufferedReader(new InputStreamReader(accept.getInputStream()));
                        //本地写
                        BufferedWriter bw = new BufferedWriter(new FileWriter("day16\\" + UUID.randomUUID().toString() + ".txt"));
                        String line;
                        while ((line = br.readLine()) != null) {
                            bw.write(line);
                        }
                        bw.flush();
                        //网络写
                        BufferedWriter bww = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
//            Scanner sc = new Scanner(System.in);
//            String s = sc.next();
                        String s = "收到";
                        bww.write(s);
                        bww.flush();
                        //accept.shutdownOutput();

                        bw.close();
                        accept.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });

        }
        // serverSocket.close();
    }
}
